package com.ninthgroup.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;

import com.ninthgroup.config.AlipayConfig;
import com.ninthgroup.entity.MessageBooking;
import com.ninthgroup.service.AliPayService;
import com.ninthgroup.service.MessageBookingService;
import com.ninthgroup.util.OrderIdUtil;
import com.ninthgroup.vo.AliPayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @param
 * @create by: 1452863884@qq.com rcg
 * @description:支付控制
 * @create time: 2022/10/24 14:27
 * @return
 */
@Controller
@RequestMapping("/AliPay")
public class AliPayController {
    /**
     * 引入一些配置信息
     */
    private static final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
    private static final String FORMAT = "JSON";
    private static final String CHARSET = "UTF-8";
    /**
     * 引入service
     */
    @Autowired
    private AliPayService aliPayService;

    @Autowired
    private MessageBookingService messageBookingService;
    //签名方式
    private static final String SIGN_TYPE = "RSA2";

    /**
     * 沙箱支付的支付请求控制器
     *
     * @param aliPayVo 请求的订单信息
     * @return
     */
    @RequestMapping("/Pay")
    public void orderPay(AliPayVo aliPayVo, HttpServletResponse response) {
        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = aliPayVo.getMIdcard();
        // 订单名称，必填
        String subject = aliPayVo.getMIdcard() + "_挂号";
        // 付款金额，必填
        String total_amount = aliPayVo.getMPrice() + "";
        // 超时时间 可空
        String timeout_express = "2m";
        // 销售产品码 必填
        String product_code = "QUICK_WAP_WAY";
        //保存订单号
        MessageBooking messageBooking = new MessageBooking();
        messageBooking.setmZfid(OrderIdUtil.getCode());
        //已支付1
        messageBooking.setmZstatus(1);
        messageBooking.setmDate(new Date());
        messageBooking.setmIdcard(aliPayVo.getMIdcard());
        messageBooking.setmTelephone(aliPayVo.getMTelephone());
        messageBooking.setmPrice(aliPayVo.getMPrice());
        messageBooking.setmAge(aliPayVo.getMAge());
        messageBooking.setcId(aliPayVo.getCId());
        if(aliPayVo.getMSex()==0){
            messageBooking.setmSex("女");
        }
        if(aliPayVo.getMSex()==1){
            messageBooking.setmSex("男");
        }

        if (aliPayVo.getMNum()<=0) {
            messageBooking.setmNum(0);
        }

        if (aliPayVo.getMNum()!=0 && aliPayVo.getMNum()>0) {
            messageBooking.setmNum(aliPayVo.getMNum()-1);
        }


        messageBookingService.insertAdd(messageBooking);


        /**********************/
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request = new AlipayTradeWapPayRequest();
        // 封装请求支付信息
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setTimeoutExpress(timeout_express);
        model.setProductCode(product_code);
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(AlipayConfig.notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(AlipayConfig.return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            response.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
            response.getWriter().write(form);//直接将完整的表单html输出到页面
            response.getWriter().flush();
            response.getWriter().close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 沙箱支付的回调控制器
     *
     * @param request  请求
     * @param response 相应
     * @return 跳转页面
     * @throws Exception 异常
     */
    @RequestMapping("/Notify")  // 注意这里必须是POST接口
    public String payNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        boolean b = aliPayService.orderPayConf(response, request);
        return "/userindex.html";
    }
}
